//package com.sg.java.apps;
//
//import com.sg.java.YhVolt2;
//import com.sg.java.entity.CMS_VOLT_CURVE;
//import com.sg.java.entity.VoltageQualifiedVo;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.sql.DriverManager;
//import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import java.text.SimpleDateFormat;
//import java.util.*;
//import java.util.stream.Collectors;
//
//import static com.sg.java.YhVolt2.initReadProperties;
//import static com.sg.java.YhVolt2.readMap;
//
//public class GetDataFromOracleToPg {
//
//    private static final Logger log = LoggerFactory.getLogger(GetDataFromOracleToPg.class);
//
//    public static final SimpleDateFormat yyyy_MM_dd          = new SimpleDateFormat("yyyy-MM-dd");
//    public static final SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//
//    static {
//        initReadProperties();
//    }
//
//    public static void main(String[] args) throws Exception {
//        String day = args[0];
//        int limit = Integer.parseInt(args[1]);
//        log.info("day：{}", day);
//        //pageSize
//        log.info("limit：{}", limit);
//        String[] DATA_DATES = day.split(",");
//        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
//        final SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
//        final String preSql = "insert into pms3_qkjdy.essential_data_" + day + " (id,sbname,sbid,date_time,dycount,uper_time_array,lower_time_array,valid_date_time,monitor_time,max_value,max_value_time,min_value,min_value_time,device_type,device_level,org_no,org_name,pid,dy_practical_count,pbtype,up_time,down_time,exception_code,voltage_level,avg,serious_high_voltage,grid_type,cons_type,voltage_fluctuation,pbarray,pbcs,new_types,high_voltage,low_voltage,severe_high_voltage,severely_low_voltage) values ";
//        log.info("preSql：{}", preSql);
//
//        Class.forName("oracle.jdbc.OracleDriver");
//
//        String oracleUrl = "jdbc:oracle:thin:@192.168.1.165:11521:helowin";
//        String oracleUser = "BEIJING_TEST";
//        String oraclePassword = "BEIJING_TEST";
//
//        Class.forName("org.postgresql.Driver");
//
//        String pgUrl = "jdbc:postgresql://piscn.vicp.cc:55432/";
//        String pgUser = "volt";
//        String pgPassword = "volt2022";
//
//        StringJoiner U1_U96 = new StringJoiner(",");
//        for (int i = 1; i <= 96; i++) {
//            U1_U96.add("U" + i);
//        }
//        String querySqlTemplate = "select " + U1_U96 + ",ID,DS,PHASE_FLAG,ORG_NO,ROWNUM from %s where DS = '%s' and ROWNUM <= %d " +
//                                  "minus " +
//                                  "select " + U1_U96 + ",ID,DS,PHASE_FLAG,ORG_NO,ROWNUM from %s where DS = '%s' and ROWNUM <= %d ";
//        String tnStr = "GDDY_AMR_VOL_CURVE";
//
//        Map<Integer, String> point_HHmmss = pointHHmmssToMap();
//
//        String finalSql = null;
//        try (java.sql.Connection oracleConn = DriverManager.getConnection(oracleUrl, oracleUser, oraclePassword);
//             java.sql.Connection pgConn = DriverManager.getConnection(pgUrl, pgUser, pgPassword)) {
//            log.info("Oracle conn初始完成");
//            log.info("pg conn初始完成");
//            for (String DATA_DATE : DATA_DATES) {
//                log.info("DATA_DATE:{}", DATA_DATE);
//                long s = System.currentTimeMillis();
//                Date date = yyyyMMdd.parse(DATA_DATE);
//                int tempReadCount;
//                //totalReadCount
//                int offset = 0;
//                int totalInsertCount = 0;
//                int round = 0;
//
////                JedisUtils.InitJedisPool("25.64.41.53", 6379, 100000, "Huawei@169");
////                Jedis jedis = JedisUtils.getJedis();
//                String querySql;
//                do {
//                    long total_s = System.currentTimeMillis();
//                    tempReadCount = 0;
//                    round++;
//                    List<CMS_VOLT_CURVE> results = new ArrayList<>(limit);
//                    Map<String, Void> METER_IDS = new HashMap<>(limit);
//                    long query_s = System.currentTimeMillis();
//                    querySql = String.format(querySqlTemplate, tnStr, DATA_DATE, offset + limit, tnStr, DATA_DATE, offset);
//                    log.info("querySql:{}", querySql);
//                    ResultSet rs = oracleConn.prepareStatement(querySql).executeQuery();
//                    long query_e = System.currentTimeMillis() - query_s;
//                    long map_s = System.currentTimeMillis();
//                    while (rs.next()) {
//                        tempReadCount++;
//                        offset++;
//                        String meterId = rs.getString(97);
//                        String dataDate = rs.getString(98);
//                        String phaseFlag = rs.getString(99);
//                        String orgNo = rs.getString(100);
//                        METER_IDS.put(meterId, null);
//                        CMS_VOLT_CURVE cms_volt_curve = new CMS_VOLT_CURVE();
//                        cms_volt_curve.setRow(meterId + "-" + dataDate + "-" + phaseFlag);
//                        cms_volt_curve.setMETER_ID(meterId);
//                        cms_volt_curve.setDATA_DATE(date);
//                        cms_volt_curve.setPHASE_FLAG(phaseFlag);
//                        cms_volt_curve.setORG_NO(orgNo);
//                        for (int i = 1; i <= 96; i++) {
//                            cms_volt_curve.getCOL_TIME$U().put(
//                                    yyyyMMddHHmmss.parse(DATA_DATE + point_HHmmss.get(i)),
//                                    rs.getString(i)
//                            );
//                        }
//                        results.add(cms_volt_curve);
//                    }
//                    long map_e = System.currentTimeMillis() - map_s;
//                    long readcsv_s = System.currentTimeMillis();
//                    readMap(YhVolt2.pbgx, YhVolt2.yhgx, YhVolt2.yhpb, METER_IDS);
//                    long readcsv_e = System.currentTimeMillis() - readcsv_s;
//                    long iterate_s = System.currentTimeMillis();
//                    final List<VoltageQualifiedVo> vos = results.stream().map(cms_volt_curve -> YhVolt2.getResult(cms_volt_curve, null)).filter(Objects::nonNull).collect(Collectors.toList());
//                    long iterate_e = System.currentTimeMillis() - iterate_s;
//                    if (!vos.isEmpty()) {
//                        long insert_s = System.currentTimeMillis();
//                        finalSql = preSql + postSql(vos);
//                        PreparedStatement ps = pgConn.prepareStatement(finalSql);
//                        int insertCount = ps.executeUpdate();
//                        totalInsertCount += insertCount;
//                        long insert_e = System.currentTimeMillis() - insert_s;
//                        log.info("此轮数：{}\t此轮读取总数：{}\t此轮计算插入数：{}\t当前已插入计算总数：{}\t此轮耗时：{}s\t查询oracle耗时：{}ms\t对象转换耗时:{}ms\t读csv耗时:{}ms\t迭代计算计算耗时：{}ms\t组装sql入pg库耗时：{}ms", round, tempReadCount, insertCount, totalInsertCount, (System.currentTimeMillis() - total_s) / 1000, query_e, map_e, readcsv_e, iterate_e, insert_e);
//                    }
//                } while (tempReadCount >= limit);
//                log.info("电压计算完毕\t总轮数：{}\t总计算插入数：{}\t总耗时：{}s", round, totalInsertCount, (System.currentTimeMillis() - s) / 1000);
////                jedis.close();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("finalSql：{}", finalSql);
//            throw new RuntimeException(e);
//        }
//    }
//
//    private static String postSql(List<VoltageQualifiedVo> vos) {
//        StringJoiner postSql = new StringJoiner(",");
//        for (VoltageQualifiedVo vo : vos) {
//            postSql.add(new StringJoiner(",", "(", ")")
//                    .add(vo.getId() != null ? "'" + vo.getId() + "'" : "null")
//                    .add(vo.getSbName() != null ? "'" + vo.getSbName() + "'" : "null")
//                    .add(vo.getSbId() != null ? "'" + vo.getSbId() + "'" : "null")
//                    .add(vo.getDateTime() != null ? "'" + yyyy_MM_dd.format(vo.getDateTime()) + "'" : "null")
//                    .add(vo.getDyCount() != null && !Double.isNaN(vo.getDyCount()) ? vo.getDyCount().toString() : "null")
//                    .add("null")
//                    .add("null")
//                    .add(vo.getEffectiveDataTime() != null && !Double.isNaN(vo.getEffectiveDataTime()) ? vo.getEffectiveDataTime().toString() : "null")
//                    .add(vo.getMonitorTime() != null && !Double.isNaN(vo.getMonitorTime()) ? vo.getMonitorTime().toString() : "null")
//                    .add(vo.getMaxValue() != null && !Double.isNaN(vo.getMaxValue()) ? vo.getMaxValue().toString() : "null")
//                    .add(vo.getMaxValueTime() != null ? "'" + yyyy_MM_dd_HH_mm_ss.format(vo.getMaxValueTime()) + "'" : "null")
//                    .add(vo.getMinValue() != null && !Double.isNaN(vo.getMinValue()) ? vo.getMinValue().toString() : "null")
//                    .add(vo.getMinValueTime() != null ? "'" + yyyy_MM_dd_HH_mm_ss.format(vo.getMinValueTime()) + "'" : "null")
//                    .add(vo.getDeviceType() != null ? "'" + vo.getDeviceType() + "'" : "null")
//                    .add(vo.getDeviceLevel() != null ? "'" + vo.getDeviceLevel() + "'" : "null")
//                    .add(vo.getOrgNo() != null ? "'" + vo.getOrgNo() + "'" : "null")
//                    .add(vo.getOrgName() != null ? "'" + vo.getOrgName() + "'" : "null")
//                    .add(vo.getPid() != null ? "'" + vo.getPid() + "'" : "null")
//                    .add(vo.getDyPracticalCount() != null && !Double.isNaN(vo.getDyPracticalCount()) ? vo.getDyPracticalCount().toString() : "null")
//                    .add(vo.getPbtype() != null ? "'" + vo.getPbtype() + "'" : "null")
//                    .add(vo.getUpTime() != null && !Double.isNaN(vo.getUpTime()) ? vo.getUpTime().toString() : "null")
//                    .add(vo.getDownTime() != null && !Double.isNaN(vo.getDownTime()) ? vo.getDownTime().toString() : "null")
//                    .add(vo.getExceptionCode() != null ? "'" + vo.getExceptionCode() + "'" : "null")
//                    .add(vo.getVoltageLevel() != null ? "'" + vo.getVoltageLevel() + "'" : "null")
//                    .add(vo.getAvg() != null && !Double.isNaN(vo.getAvg()) ? vo.getAvg().toString() : "null")
//                    .add(vo.getSeriousnessPb() != null ? "'" + vo.getSeriousnessPb() + "'" : "null")
//                    .add(vo.getGridType() != null ? "'" + vo.getGridType() + "'" : "null")
//                    .add(vo.getConsType() != null ? "'" + vo.getConsType() + "'" : "null")
//                    .add(vo.getVoltageFluctuation() != null ? "'" + vo.getVoltageFluctuation() + "'" : "null")
//                    .add(vo.getPbArray() != null ? "'" + vo.getPbArray() + "'" : "null")
//                    .add(vo.getPbcs() != null ? "'" + vo.getPbcs() + "'" : "null")
//                    .add(vo.getNewType() != null ? "'" + vo.getNewType() + "'" : "null")
//                    .add(vo.getHighVoltage() != null ? "'" + vo.getHighVoltage() + "'" : "null")
//                    .add(vo.getLowVoltage() != null ? "'" + vo.getLowVoltage() + "'" : "null")
//                    .add(vo.getSevereHighVoltage() != null ? "'" + vo.getSevereHighVoltage() + "'" : "null")
//                    .add(vo.getSeverelyLowVoltage() != null ? "'" + vo.getSeverelyLowVoltage() + "'" : "null")
//                    .toString())
//            ;
//        }
//        return postSql.toString();
//    }
//
//    public static Map<Integer, String> pointHHmmssToMap() {
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(Calendar.HOUR_OF_DAY, 0);
//        calendar.set(Calendar.MINUTE, -15);
//        calendar.set(Calendar.SECOND, 0);
//        calendar.set(Calendar.MILLISECOND, 0);
//        SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
//        Map<Integer, String> point_HHmmssMap = new HashMap<>();
//        for (int i = 1; i <= 96; i++) {
//            calendar.add(Calendar.MINUTE, 15);
//            point_HHmmssMap.put(i, sdf.format(calendar.getTime()));
//        }
//        return point_HHmmssMap;
//    }
//
//}
